package com.dingding.order.processer.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.UserLogin;
import com.dingding.common.core.beans.UserTaxiOrdert;
import com.dingding.common.core.beans.openManager.Systemconfig;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.config.SystemConfig;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.common.core.util.DateFormatUtil;
import com.dingding.order.code.order.PublishCarType;
import com.dingding.order.core.common.IUserFormalOrderDao;
import com.dingding.order.core.common.IUserOrderDao;
import com.dingding.order.core.common.IUserTaxiOrderDao;
import com.dingding.order.core.march.dao.IMarchUserOrderTDao;
import com.dingding.order.core.myorder.dao.IGetMyOrderDao;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.march.underway.UderWayOrderDto;
import com.dingding.order.dto.myorder.MyOrder;
import com.dingding.order.dto.myorder.ReqMyOrderDTO;
import com.dingding.order.dto.pay.paymoney.GetThirdPayAndAccountDto;
import com.dingding.order.processer.IOrderProcesser;

/**
 * @Description:出租车
 * @author fengshuonan
 * @date 2016年8月18日 下午5:28:49
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("taxiOrderProcesser")
public class TaxiOrderProcesser implements IOrderProcesser {

	@Resource(name = "userTaxiOrderDao")
	private IUserTaxiOrderDao userTaxiOrderDao;

	@Resource(name = "userFormalOrderDao")
	private IUserFormalOrderDao userFormalOrderDao;

	@Resource(name = "marchUserOrderTDao")
	private IMarchUserOrderTDao marchUserOrderTDao;

	@Resource(name = "getMyOrderDao")
	private IGetMyOrderDao getMyOrderDao;

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;

	@Resource(name = "userOrderCommonDao")
	private IUserOrderDao userOrderDao;

	/**
	 * @Description:获取订单状态（出租车的）
	 * @author fengshuonan
	 * @return int (0:无此订单 1：待抢单 2：服务中 3：待支付 4：已完成 5：已取消)
	 * @since 2.4
	 */
	@Override
	public int getOrderStatus(String orderId) {
		Integer status = 0;
		status = this.userTaxiOrderDao.getUserTaxiOrderStatus(orderId);
		if (status == null) {

			// 如果临时表中没有这个订单状态，可能是该订单已经支付，该订单已经在正式表中
			status = this.userFormalOrderDao.getFormalOrderStatus(orderId);
			if (status == null) {
				status = 0;
			}
		}
		return status;
	}

	@Override
	public GetThirdPayAndAccountDto getThirdPayAndAccountPay(BigDecimal accountRemain, String orderId) {

		BigDecimal orderMoney = new BigDecimal(0);

		UserTaxiOrdert userTaxiOrdert = this.userTaxiOrderDao.getById(orderId);

		if (userTaxiOrdert == null) {
			// 从正式库中查询
			Userspecialorder zorder = this.baseDaoX.getById(orderId, Userspecialorder.class);
			if (zorder != null && zorder.getOrderStatus() != null) {
				orderMoney = zorder.getTotalMoney();
			}
		} else {
			orderMoney = userTaxiOrdert.getTotalMoney();
		}

		if (orderMoney == null) {
			return null;
		}

		// 判断是否有优惠金额，如果有减掉优惠金额
		// if (userTaxiOrdert.getCouponMoney() != null) {
		// orderMoney.subtract(new BigDecimal(userTaxiOrdert.getCouponMoney()));
		// }

		GetThirdPayAndAccountDto dto = new GetThirdPayAndAccountDto();

		if (!SystemConfig.isEnv_system()) {
			dto.setCouponMoney(6);
			orderMoney.subtract(new BigDecimal(6));
		}
		if (accountRemain.compareTo(orderMoney) < 0) {
			dto.setAccountPayMoney(accountRemain);
			dto.setThirdPayMoney(orderMoney.subtract(accountRemain));
		} else {
			dto.setAccountPayMoney(orderMoney);
			dto.setThirdPayMoney(new BigDecimal(0));
		}

		return dto;
	}

	@Override
	public List<UderWayOrderDto> getUnderWayOrders(String userId, Integer clientType) {
		return this.marchUserOrderTDao.getTaxiUnderWayOrder(userId);
	}

	@Override
	public List<HistoryOrderDto> getHistoryOrders(String userId, int orderStatus, int currentPage, int pageSize,
			Integer userType) {
		if (orderStatus == 5) {// 如果是已完成订单
			orderStatus = 4;
		} else if (orderStatus == 6) {// 如果是已取消
			orderStatus = 5;// 已取消
		}
		ReqMyOrderDTO reqMyOrderDTO = new ReqMyOrderDTO();
		reqMyOrderDTO.setOrderStatus(orderStatus);
		reqMyOrderDTO.setPageIndex(currentPage);
		reqMyOrderDTO.setPageSize(pageSize);
		reqMyOrderDTO.setUserId(userId);
		List<MyOrder> myOrders = null;

		if (userType == 1) { // 司机
			myOrders = this.getMyOrderDao.getMyDriverOrderList(reqMyOrderDTO);
		} else if (userType == null || userType == 2) { // 乘客
			myOrders = this.getMyOrderDao.getMyOrderList(reqMyOrderDTO);
		}

		if (myOrders == null) {
			return new ArrayList<HistoryOrderDto>();
		}
		List<HistoryOrderDto> historyOrderDtos = new ArrayList<HistoryOrderDto>();
		for (MyOrder order : myOrders) {
			HistoryOrderDto historyOrderDto = new HistoryOrderDto();
			BeanUtils.copyProperties(order, historyOrderDto);
			historyOrderDto.setSetOutTime(DateFormatUtil.yyyy_MM_dd_HHmmss(order.getSetOutTime()));

			// 取消原因 1:自己取消 2:对方取消取消 3:自动取消
			historyOrderDto.setCancelType(historyOrderDto.getCancelRerson());

			// 发布订单类型
			historyOrderDto.setPublishType(order.getRegistrationType());

			// 遍历所有出租车订单记录到集合并返回
			if (order.getRegistrationType() == PublishCarType.TAXI.getCode()) {
				historyOrderDto.setPublishType(PublishCarType.TAXI.getCode());
				historyOrderDtos.add(historyOrderDto);
			}
		}
		return historyOrderDtos;
	}

	@Override
	public String getCalledUserTell(String orderId, String userId) {

		UserTaxiOrdert ordert = this.baseDaoX.getById(orderId, UserTaxiOrdert.class);

		if (ordert == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_ORDER);
		}

		String calledUserId = null;

		// 如果获取被叫电话号码时，用户id为下单的用户id，则获取被叫电话号码为抢单用户id的电话号码
		if (userId.equals(ordert.getPlaceOrderUserId())) {
			calledUserId = ordert.getGrabOrderUserId();
			Specialuserlogin byId = this.baseDaoX.getById(calledUserId, Specialuserlogin.class);
			return byId.getUserName();
		} else {
			calledUserId = ordert.getPlaceOrderUserId();
			UserLogin byId = this.baseDaoX.getById(calledUserId, UserLogin.class);
			return byId.getUserName();
		}

	}

	@Override
	public String getUserTellByUserId(String userId) {

		UserLogin userlogin = this.baseDaoX.getById(userId, UserLogin.class);

		if (userlogin == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_USER);
		}

		return userlogin.getUserName();
	}

	@Override
	public String getCallUserTell(String orderId, String userId) {

		UserTaxiOrdert ordert = this.baseDaoX.getById(orderId, UserTaxiOrdert.class);

		if (ordert == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_ORDER);
		}

		// 如果获取被叫电话号码时，用户id为下单的用户id，则获取被叫电话号码为抢单用户id的电话号码
		if (userId.equals(ordert.getPlaceOrderUserId())) {
			UserLogin byId = this.baseDaoX.getById(userId, UserLogin.class);
			return byId.getUserName();
		} else {
			Specialuserlogin byId = this.baseDaoX.getById(userId, Specialuserlogin.class);
			return byId.getUserName();
		}
	}

	@Override
	public List<HistoryOrderDto> completeAndCancelOrder(String userId, int currentPage, int pageSize,
			Integer userType) {
		// TODO Auto-generated method stub
		return null;
	}

}
